﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyCommon.Helper;
using MyCommon.HttpContextUser;
using MyIServices;
using MyModels;
using MyModels.Models;
using MyServices;

namespace MyApi.Controllers
{
    /// <summary>
    /// 部门控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize("Permission")]
    public class DepartmentController : BaseApiController
    {
        private readonly IDepartmentService _departmentService;
        private readonly IUser _user;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="departmentService"></param>
        public DepartmentController(IDepartmentService departmentService,IUser user)
        {
            _departmentService = departmentService;
            _user = user;
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<Department>>> Get(int page = 1, int pageSize = 10, string key = "")
        {
            var list = await _departmentService.QueryPage(i=>i.IsDeleted==false&&(i.Name!=null&&i.Name.Contains(key)), page, pageSize, " Id desc");
            return Success(list);
        }

        /// <summary>
        /// 添加部门数据
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post(Department department)
        {
            department.CreateId =_user.UserId;
            department.CreateBy = _user.UserName;
            var id = await _departmentService.Add(department);
            return id > 0 ? Success(id.ToString(), "添加成功") : Fail("添加失败");
        }

        /// <summary>
        /// 根据id获取一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<Department>> GetById(long id)
        {
            var data = await _departmentService.QueryById(id);
            if (data != null) {
                return Success(data, "获取成功");
            }
            return Fail<Department>("未查找到数据");
        }



        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>>Put(Department department)
        {
            department.ModifyBy = _user.UserName;
            department.ModifyId = _user.UserId;
            var res = await _departmentService.Update(department);
            return res ? Success(department.Id.ToString(), "修改成功") : Fail("修改失败");
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(long id)
        {
            if (id <= 0) return Fail("参数错误");
            var model = await _departmentService.QueryById(id);
            model.IsDeleted = true;
            var res = await _departmentService.Update(model);
            return res ? Success(id.ToString(), "删除成功") : Fail("删除失败");
        }

        /// <summary>
        /// 获取部门树
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<DepartmentTree>> GetDepartmentTree(long pid = 0)
        {
            var departments = await _departmentService.Query(d => d.IsDeleted == false);
            var departmentTrees = (from child in departments
                                   where child.IsDeleted == false
                                   orderby child.Id
                                   select new DepartmentTree
                                   {
                                       value = child.Id,
                                       label = child.Name,
                                       Pid = child.Pid,
                                       order = child.OrderSort,
                                   }).ToList();
            DepartmentTree rootRoot = new DepartmentTree
            {
                value = 0,
                Pid = 0,
                label = "根节点"
            };

            departmentTrees = departmentTrees.OrderBy(d => d.order).ToList();


            RecursionHelper.LoopToAppendChildren(departmentTrees, rootRoot, pid);

            return Success(rootRoot, "获取成功");
        }
    }
}
